<template>
  <div>
    <el-dialog v-model="props.dialogVisible" title="选择分类" @close="cannel">
      <el-form ref="elFormRef" :model="formData" :rules="rules" size="medium" >
        <el-row :gutter="15">
            <el-col :span="14">
              <el-form-item label="" prop="classifyName">
                <el-input v-model="formData.classifyName" placeholder="请输入分类名称" clearable :style="{ width: '100%' }">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="flex_one_box tr">
                <el-button type="primary" size="default" @click="handleQuery">搜索</el-button>
                <el-button size="default" @click="resetQuery">重置</el-button>
              </div>
            </el-col>
          <el-col :span="14" style="color:#000; margin: 0 0 20px;">所有分类</el-col>
          <el-col :span="24">
            <el-cascader-panel 
              :filterable="true" 
              :options="allClassList" 
              v-model="props.idsList"
              :props="{
                label:'classifyName',
                value:'id',
                multiple:true,
                children:'itemList',
                checkStrictly:true,
              }"
              @change="change"
              ></el-cascader-panel>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" style="padding-top: 15px;">
        <el-button @click="cannel" size="default">取消</el-button>
        <el-button type="primary" size="default" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch,onUpdated } from 'vue'
import { getClassifytree } from "@/api/goods";

// 接收props
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
  onOk: {
    type: Function,
    default: () => {}
  },
  idsList: {
    type: Array,
    default: []
  },
  onClose: {
    type: Function,
    default: () => {}
  }
})
const idsList = ref([])
// 定义emits
const emits = defineEmits(['update:dialogVisible', 'onOk', 'onClose','idsList'])

// 数据状态
const elFormRef = ref(null)

const formData = reactive({
  classifyName: undefined,
})

const rules = reactive({})

// 查询参数
const allClassList = ref([])
// 获取所有商品分类
const getAllList = () => {
  const data  = {
    ...formData,
    current: 1,
    size: 999,
    type:'1',// 1-商品分类
  }
  getClassifytree(data)
  .then(res => {
    let arr = res.data.records
    // arr.map(item=>{// 只展示二级
    //   if(item.itemList?.length){
    //     item.itemList.map(item2=>{
    //       item2.itemList = null
    //     })
    //   }
    // })
    allClassList.value = arr
  })
  .finally(() => {
  })
}
getAllList()
// 生命周期钩子
onMounted(() => {
  console.log(112, props.dialogVisible)
})
onUpdated(()=>{
  console.log(113, props)
})
const change = (val,val2)=>{
  let list = []
  val.forEach(item=>{
    if(item.length){
      list.push(item[item.length-1])
    }
  })
  console.log(1234,list)
  idsList.value = list
}
// 方法
const cannel = () => {
  emits('onClose', false)
}

const handleConfirm = () => {
  console.log(11,idsList)
  emits('onOk', idsList)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getAllList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  resetForm("queryForm")
  handleQuery()
}
const resetForm = (formName) => {
  // 实际项目中实现
}
</script>

<style lang="scss">
.flex_one_box{
  height: 36px;
  display: flex;
}
</style>